package org.example.test;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
 
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 调用企业微信api，获取打卡记录
 * 1、获取企业微信token
 * 获取access_token - 接口文档 - 企业微信开发者中心
 *
 * 请求方式：POST（HTTPS）
 * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token=ACCESS_TOKEN
 *
 * 2、获取所有人员数据
 * 获取部门成员 - 接口文档 - 企业微信开发者中心
 *
 * 请求方式：GET（HTTPS）
 * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=ACCESS_TOKEN&department_id=1&fetch_child=1
 *
 * 3、获取打卡记录数据
 * ​​​​​​获取打卡记录数据 - 接口文档 - 企业微信开发者中心
 *
 * 请求方式：POST（HTTPS）
 * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token=ACCESS_TOKEN
 */
public class HttpUtil {
    public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final String Corpid = "·";//替换成自己的企业ID
    public static final String Corpsecret = "应用Secret";//替换成打卡应用的secret
    public static final String GetToken = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
    public static final String GetCheckindata = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata";
 
    public static final String GetUserlist = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist";
 
    public static final Integer MAX_NUM = 100;//企业微信打卡每次最多取100条记录
 
    public static void main(String[] args) throws Exception {
        String rq = GetToken + "?Corpid=" + Corpid + "&Corpsecret=" + Corpsecret;
        //get请求获取数据，获取token
        JSONObject gettokenRs = JSONObject.parseObject(doGet(rq));
        String token = gettokenRs.getString("access_token");//获取token
 
        JSONObject body = new JSONObject();
        List<String> idList = new ArrayList<>();
        //获取所有人员信息
        JSONObject getuserlistRs = JSONObject.parseObject(doGet(GetUserlist+ "?access_token=" + token+"&department_id=1&fetch_child=1"));
        JSONArray userlist = getuserlistRs.getJSONArray("userlist");
        if(userlist==null){
            System.out.println("userlist为空");
            return;
        }
        final int ulsize = userlist.size();
        for (int i = 0; i < ulsize; i++) {
            JSONObject udata = userlist.getJSONObject(i);
            String depuserId = udata.getString("userid");
            idList.add(depuserId);
        }
        //每100人为1组取数据
        final int idlsize = idList.size();
        //j+= MAX_NUM  这一行的目的是在每次处理完一个子列表后，将起始索引 j 向前移动100个位置，
        // 以便在下一次处理时从新的起始位置开始提取下一个子列表。这种操作通常用于分批处理一个较大的列表，例如在批量处理数据库查询结果或进行批量操作时
        for (int j = 0 ; j < idlsize; j+= MAX_NUM) {
            //idList 中提取一个子列表，该子列表从索引 j 开始，最多包含100个元素，如果从索引 j 开始的元素不足100个，则包含所有剩余的元素
            List<String> useridlist = idList.subList(j, j+MAX_NUM>idList.size()?idList.size():j+MAX_NUM);
            body.put("opencheckindatatype", "3");
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.add(Calendar.DATE, -1);
            body.put("starttime", calendar.getTimeInMillis() / 1000);
            calendar.add(Calendar.DATE, 2);
            body.put("endtime", calendar.getTimeInMillis() / 1000);
            body.put("useridlist", useridlist);
            JSONObject checkobj = JSONObject.parseObject(doPost(GetCheckindata + "?access_token=" + token, body));//取打卡数据
            JSONArray checkindata = checkobj.getJSONArray("checkindata");
            final int ckdsize = checkindata.size();
            for (int i = 0; i < ckdsize; i++) {
                JSONObject data = checkindata.getJSONObject(i);
                String userId = data.getString("userid");
                String type = data.getString("checkin_type");
                String location = data.getString("location_detail");//打卡地点
                String longitude = data.getString("lng");
                String latitude = data.getString("lat");
                Long ckTime = data.getLong("checkin_time");//打卡时间毫秒数
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateTime = sdf.format(ckTime * 1000);//毫秒数转换成日期格式
                String sql = "insert into HrCardData (userId,cardType,cardDate,cardTime,addr,longitude,latitude) values ('" + userId + "','" + type + "','" + dateTime.split(" ")[0] + "','" + dateTime.split(" ")[1] + "','" + location + "','" + longitude + "','" + latitude + "')";
                System.out.println(sql);
            }
        }
    }
 
 
    /**
     * get请求
     *
     * @param getUrl
     * @return
     */
    public static String doGet(String getUrl) {
        try {
            URL url = new URL(getUrl);
            //设置连接方式
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            //设置主机连接时间超时时间3000毫秒
            conn.setConnectTimeout(3000);
            //设置读取远程返回数据的时间3000毫秒
            conn.setReadTimeout(3000);
            //发送请求
            conn.connect();
            //获取输入流
            InputStream is = conn.getInputStream();
            //封装输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            //接收读取数据
            StringBuffer sb = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line);
                sb.append("\r\n");
            }
            if (null != br) {
                br.close();
            }
            if (null != is) {
                is.close();
            }
            //关闭连接
            conn.disconnect();
            return sb.toString();
        } catch (Exception e) {
            e.getMessage();
        }
        return "";
    }
 
    /**
     * 原生http请求
     *
     * @param sendUrl 请求的Url
     * @param body    传入的参数
     * @return
     */
    public static String doPost(String sendUrl, JSONObject body) {
        OutputStreamWriter out = null;
        BufferedReader in = null;
        StringBuilder result = new StringBuilder();
        HttpURLConnection conn = null;
        try {
            URL url = new URL(sendUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            //发送POST请求必须设置为true
            conn.setDoOutput(true);
            conn.setDoInput(true);
            //设置连接超时时间和读取超时时间
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(10000);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Accept", "application/json");
            //获取输出流
            out = new OutputStreamWriter(conn.getOutputStream());
            out.write(body.toJSONString());
            out.flush();
            out.close();
            //取得输入流，并使用Reader读取
            if (200 == conn.getResponseCode()) {
                in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
                String line;
                while ((line = in.readLine()) != null) {
                    result.append(line);
                    System.out.println(line);
                }
            } else {
                System.out.println("ResponseCode is an error code:" + conn.getResponseCode());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
        return result.toString();
    }
 
    /**
     * 将map转String
     *
     * @param map
     * @return
     */
    private static String mapToString(Map<String, String> map) {
        StringBuilder sb = new StringBuilder();
        int index = 0;
        for (String i : map.keySet()) {
            index++;
            if (map.size() == index) {
                sb.append(i + "=" + map.get(i));
            } else {
                sb.append(i + "=" + map.get(i) + "&");
            }
        }
        return sb.toString();
    }
}